package com.zb.controller.settings;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.zb.common.annotation.Permission;
import com.zb.common.annotation.WriteLog;
import com.zb.common.exception.BusinessException;
import com.zb.common.po.enums.ResponseCode;
import com.zb.common.po.enums.RoleStatus;
import com.zb.common.po.model.Menu;
import com.zb.common.po.model.Role;
import com.zb.common.po.query.RoleQuery;
import com.zb.common.po.vo.AjaxResponse;
import com.zb.common.utils.PaginationResult;
import com.zb.service.RoleService;

@Controller
@RequestMapping(value = "/settings")
public class RoleSettingsController {

    Logger logger = LoggerFactory.getLogger(RoleSettingsController.class);

    @Resource
    private RoleService roleService;

    /**
     * 角色列表 roleList:(这里用一句话描述这个方法的作用)
     * 
     * @author luohaili
     * @return
     * @since JDK 1.7
     */
    @Permission(code = "role:list")
    @RequestMapping(value = "/role_list.do")
    public String roleList() {
        return "page/settings/role_list";
    }

    @Permission(code = "role:list")
    @ResponseBody
    @RequestMapping(value = "/loadRoleList.do")
    public AjaxResponse<PaginationResult<Role>> loadRoleList(
            HttpSession session, RoleQuery query) {
        AjaxResponse<PaginationResult<Role>> response = new AjaxResponse<PaginationResult<Role>>();
        try {
            query.setStatus(RoleStatus.NORMAL);
            PaginationResult<Role> result = roleService.findRolesbyPage(query);
            response.setData(result);
            response.setResponseCode(ResponseCode.SUCCESS);
        } catch (Exception e) {
            logger.error("查询角色失败：{}", e);
            response.setResponseCode(ResponseCode.SERVERERROR);
            response.setErrorMsg("查询角色异常");
        }
        return response;
    }

    /**
     * 新增角色 toSaveRole:(这里用一句话描述这个方法的作用)
     * 
     * @author luohaili
     * @param roleId
     * @return
     * @since JDK 1.7
     */
    @Permission(code = "role:add")
    @RequestMapping(value = "/role_2add.do")
    public ModelAndView role_2add() {
        ModelAndView result = new ModelAndView();
        try {
            result.setViewName("page/settings/role_edit");
        } catch (Exception e) {
            result.setViewName("page/error/500");
        }
        return result;
    }

    @Permission(code = "role:update")
    @RequestMapping(value = "/role_2update.do")
    public ModelAndView role_2update(Integer roleId) {
        ModelAndView result = new ModelAndView();
        try {
            Role role = roleService.getRoleById(roleId);
            result.addObject("role", role);
            result.setViewName("page/settings/role_edit");
        } catch (Exception e) {
            logger.info("获取角色信息异常:{}", e);
            result.setViewName("page/error/500");
        }
        return result;
    }

    @ResponseBody
    @RequestMapping(value = "/load_menue4_role.do")
    public AjaxResponse<List<Menu>> loadMenue4Role(HttpSession session,
            Integer roleId) {
        AjaxResponse<List<Menu>> response = new AjaxResponse<List<Menu>>();
        try {
            List<Menu> menuList = roleService.findRole2MenuByRoleId(roleId);
            response.setResponseCode(ResponseCode.SUCCESS);
            response.setData(menuList);
        } catch (Exception e) {
            logger.error("获取菜单信息异常：{}", e);
            response.setResponseCode(ResponseCode.SERVERERROR);
            response.setErrorMsg("获取菜单信息异常");
        }
        return response;
    }

    @RequestMapping(value = "/role_showRole.do")
    public ModelAndView role_showRole(
            @RequestParam(value = "roleId") Integer roleId) {
        ModelAndView result = new ModelAndView();
        try {
            if (null != roleId) {
                Role role = roleService.getRoleById(roleId);
                result.addObject("role", role);
            }
            result.setViewName("page/settings/role_show");
        } catch (Exception e) {
            logger.info("获取角色信息异常:{}", e);
            result.setViewName("error/500");
        }
        return result;
    }

    @ResponseBody
    @RequestMapping(value = "/load_menue4_show_role.do")
    public AjaxResponse<List<Menu>> load_menue4_show_role(HttpSession session,
            Integer roleId) {
        AjaxResponse<List<Menu>> response = new AjaxResponse<List<Menu>>();
        try {
            List<Menu> menuList = roleService.findRole2MenuByRoleId(roleId);
            for (Menu menu : menuList) {
                menu.setChkDisabled(true);
            }
            response.setResponseCode(ResponseCode.SUCCESS);
            response.setData(menuList);
        } catch (Exception e) {
            logger.error("获取菜单信息异常：{}", e);
            response.setResponseCode(ResponseCode.SERVERERROR);
            response.setErrorMsg("获取菜单信息异常");
        }
        return response;
    }

    @Permission(code = "role:add")
    @ResponseBody
    @RequestMapping(value = "/role_add.do")
    @WriteLog(opObject = "角色", opDescription = "新增角色：#{#param0},权限菜单：#{#param1}", paramIndexes = {
            1, 2 })
    public AjaxResponse<?> role_add(HttpSession session, Role role,
            Integer[] menuIds) {
        AjaxResponse<?> response = new AjaxResponse<Object>();
        try {
            roleService.saveRole(role, menuIds);
            response.setResponseCode(ResponseCode.SUCCESS);
        } catch (BusinessException e) {
            logger.error("保存角色失败：{}", e);
            response.setResponseCode(ResponseCode.BUSINESSERROR);
            response.setErrorMsg(e.getMessage());
        } catch (Exception e) {
            logger.error("保存角色失败：{}", e);
            response.setResponseCode(ResponseCode.SERVERERROR);
            response.setErrorMsg("保存角色失败");
        }
        return response;
    }

    @Permission(code = "role:update")
    @ResponseBody
    @RequestMapping(value = "/role_update.do")
    @WriteLog(opObject = "角色", opDescription = "修改角色：#{#param0},权限菜单：#{#param1}", paramIndexes = {
            1, 2 })
    public AjaxResponse<?> role_update(HttpSession session, Role role,
            Integer[] menuIds) {
        AjaxResponse<?> response = new AjaxResponse<Object>();
        try {
            roleService.saveRole(role, menuIds);
            response.setResponseCode(ResponseCode.SUCCESS);
        } catch (BusinessException e) {
            logger.error("保存角色失败：{}", e);
            response.setResponseCode(ResponseCode.BUSINESSERROR);
            response.setErrorMsg(e.getMessage());
        } catch (Exception e) {
            logger.error("保存角色失败：{}", e);
            response.setResponseCode(ResponseCode.SERVERERROR);
            response.setErrorMsg("保存角色失败");
        }
        return response;
    }

    /**
     * 删除角色 delRole:(这里用一句话描述这个方法的作用)
     * 
     * @author luohaili
     * @param session
     * @param ids
     * @return
     * @since JDK 1.7
     */
    @Permission(code = "role:del")
    @ResponseBody
    @RequestMapping(value = "/delRole.do")
    @WriteLog(opObject = "角色", opDescription = "删除角色：角色id[#{#param0}]", paramIndexes = { 1 })
    public AjaxResponse<?> delRole(HttpSession session, String[] ids) {
        AjaxResponse<?> response = new AjaxResponse<Object>();
        try {
            roleService.deleteRoleBatch(ids);
            response.setResponseCode(ResponseCode.SUCCESS);
        } catch (BusinessException e) {
            logger.error("删除角色失败：{}", e);
            response.setResponseCode(ResponseCode.BUSINESSERROR);
            response.setErrorMsg(e.getMessage());
        } catch (Exception e) {
            logger.error("删除角色失败：{}", e);
            response.setResponseCode(ResponseCode.SERVERERROR);
            response.setErrorMsg("删除角色失败");
        }
        return response;
    }

}
